package com.whately.core.base.utils;

import com.fasterxml.uuid.Generators;
import com.whately.core.base.keygen.WhatelyMachineCodeHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class UUIDUtils {


    public static String strongId(){
        return strongId(DateUtil.nowMilliSecond(),IntegerUtils.random(100,199));
    }

    public static List<String> strongIdBatch(long milliSecond, int count){
        if(count>1&&count<=800){
            int randomNumber = IntegerUtils.random(100,199);
            List<String> ls=new ArrayList<>(count);
            for(int i =0 ;i<count;i++){
                String id =  strongId(milliSecond,randomNumber++);
                ls.add(id);
            }
            return ls;
        }else {
            throw new RuntimeException("batch count must lower than 800 once !");
        }

    }


    /**
     * 获得一个UUID
     * version4 ,通过随机数产生
     * 有重复的可能性
     */
    public static String randomUUID(){
        String uuid = UUID.randomUUID().toString();
        //去掉“-”符号
        return uuid.replaceAll("-", "");
    }

    /**
     * 共46位
     * 毫秒的52进制 8位 +分隔符1位 +机器码1位+3位随机+32位uuid+3位随机睡
     */
    private static String strongId(long milliSecond, int randomNumber){
        return new StringBuffer().append(RadixNumberUtils.convertToChars(milliSecond)).append("0").append(WhatelyMachineCodeHelper.getMachineCode() ).append(randomNumber).append(randomUUID()).toString();
    }


    /**
     * 获得一个UUID
     * version1 ,通过时钟+mac（ip）产生
     * 有暴露mac的风险
     * 重复可能为零
     */
    private static String timestampUUID(){
        UUID uuidGene = Generators.timeBasedGenerator().generate();
        String uuid = uuidGene.toString();
        //去掉“-”符号
        return uuid.replaceAll("-", "");
    }

}
